<!--
 * @Author: wanchunyi 1536793586@qq.com
 * @Date: 2022-11-19 15:07:46
 * @LastEditors: wanchunyi 1536793586@qq.com
 * @LastEditTime: 2023-02-22 15:49:49
 * @FilePath: \virtualHome\src\views\login.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->

<template>
  <div class="login_container" v-loading="loading">
    <div class="login_box">
      <!-- <div class="ava_box">
        <img src="@/assets/logo.png" />
      </div> -->

      <!-- 账号 -->
      <el-form
        :model="loginForm"
        ref="loginFormRef"
        :rules="loginFormRules"
        label-width="0px"
        class="login_form"
      >
        <el-form-item prop="username">
          <el-input
            placeholder="请输入账号"
            v-model="loginForm.username"
            prefix-icon="el-icon-user-solid"
          ></el-input>
        </el-form-item>

        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            placeholder="请输入密码"
            v-model="loginForm.password"
            :show-password="true"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-input
            placeholder="请输入验证码"
            v-model="loginForm.code"
            @keyup.enter.native="login"
          ></el-input>
        </el-form-item>
        <!-- 按钮 -->
        <div
          class="login-code"
          style="
            cursor: pointer;
            width: 40%;
            height: 48px;
            float: left;
            background-color: #f0f1f5;
          "
        >
          <img
            style="
              height: 48px;
              width: 100%;
              border: 1px solid rgba(0, 0, 0, 0.1);
              border-radius: 5px;
            "
            :src="codeUrl"
            @click="getCode"
          />
        </div>
        <el-form-item class="btns">
          <el-button type="primary" @click="login" round>登录</el-button>
          <!--      <el-button type="success" round>成功按钮</el-button> -->
          <el-button type="info" @click="resetLoginForm" round>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="boxversion">版本号：{{ VUE_APP_VERSION }}</div>
  </div>
</template>
 
<script>
import { getCodeImg } from "@/api/user";

// import {  } from "../../babel.config";
export default {
  data() {
    return {
      VUE_APP_VERSION: process.env.VUE_APP_VERSION,
      loading: false,
      codeUrl: "",
      // 表单数据绑定
      loginForm: {
        // username: "18177949610",
        // password: "123456",
        username: "",
        password: "",
        // Rmv9ULjy6x
        loginType: 1, // 面向前端用户
        tenant: 10000, //多租户的id
      },
      // 检验规则
      loginFormRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 1,
            max: 11,
            message: "长度在 8 到 11 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 12,
            message: "长度在 6 到 12 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.getCode();
  },
  methods: {
    resetLoginForm() {
      this.$refs.loginFormRef.resetFields();
    },
    login() {
      this.loading = true;
      this.$refs.loginFormRef.validate(async (valid) => {
        if (valid) {
          this.$store
            .dispatch("user/login", {userInfo:this.loginForm,VUE_APP_VERSION:this.VUE_APP_VERSION})
            .then(() => {
              this.$router.push(
                {
                  path: this.redirect || "/",
                  query: this.otherQuery,
                },
                () => {}
              );

              this.loading = false;
              this.$message({
                message: "登入成功",
                type: "success",
              });
            })
            .catch((err) => {
              this.$message({
                message: err,
                type: "error",
              });
              this.getCode();
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    getCode() {
      getCodeImg().then((res) => {
        if (res !== undefined) {
          this.codeUrl = res.data;
          this.loginForm.uuid = res.id;
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.login_container {
  background-image: url("../assets/SignUI.jpg");
  background-color: #2b4b6b;
  height: 100%;
  background-size: 100% 100%;
  position: relative;
  .boxversion {
    position: absolute;
    bottom: 15%;
    right: 15%;
    color: rgb(64, 158, 255);
  }
}
.login_box {
  height: 35%;
  width: 25%;
  background-color: #fff;
  border-radius: 3px;
  position: absolute;
  left: 54%;
  top: 32%;
  // 横轴，纵轴
  // transform: translate(-50%, -50%);

  .ava_box {
    height: 130px;
    width: 130px;
    border: 1px solid #eee;
    border-radius: 50%;
    padding: 10px;
    box-shadow: 0 0 10px #ddd;
    position: absolute;
    left: 50%;
    transform: translate(-50%, -50%);
    background-color: #fff;

    img {
      width: 100%;
      height: 100%;
      border-radius: 50%;
      background-color: #eee;
    }
  }
  .btns {
    display: flex;
    justify-content: flex-end;
  }
  .login_form {
    position: absolute;
    bottom: 0px;
    width: 100%;
    padding: 0 20px;
    box-sizing: border-box;
  }
}
</style>